package com.soujiayi.adapter;

import java.util.List;
import java.util.Map;

import com.soujiayi.activity.R;
import com.soujiayi.activity.R.id;
import com.soujiayi.activity.R.layout;
import com.soujiayi.activity.R.string;
import com.soujiayi.util.Constants;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class SearchListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<? extends Map<String, ?>> data;
	private String CLEAR_SEARCH_HISTORY;

	public SearchListAdapter(Context context,List<? extends Map<String, ?>> data) {
		this.data = data;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		CLEAR_SEARCH_HISTORY = context.getResources().getString(
				R.string.clear_search_history);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	public String get(int position, Object key) {
		Map<String, ?> map = (Map<String, ?>) getItem(position);
		return map.get(key).toString();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		Map<String, ?> item = data.get(position);
		String key = item.get(Constants.SEARCH_LIST_ITEM).toString();
		if(!key.equals(Constants.SEARCH_LIST_CLEAR_HISTORY_ITEM)){
			convertView = mInflater.inflate(R.layout.product_search_list_item, null); 
			TextView searchKeyItem = (TextView)convertView.findViewById(R.id.product_search_list_searchItem);
			searchKeyItem.setText(key);
		}else{
			convertView = mInflater.inflate(R.layout.product_search_list_clear_history_item, null); 
			TextView clearHistoryItem = (TextView)convertView.findViewById(R.id.product_search_searchHistoryItem);
			clearHistoryItem.setText(this.CLEAR_SEARCH_HISTORY);
		}
		convertView.setTag(position);

		return convertView;
	}


}
